﻿using BP.AIC.Framwork.Repository;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Db;

public static class ServiceCollectionExtension
{
    /// <summary>
    /// 添加数据库连接
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    /// <param name="section"></param>
    /// <returns></returns>
    public static IServiceCollection AddSqlSugarConnection(this IServiceCollection services, IConfiguration configuration, string section = "DbConfig")
    {
        var configs = configuration.GetSection(section).Get<List<ConnectionConfig>>();
        //foreach (var item in configs)
        //{
        //    item.ConfigureExternalServices = new ConfigureExternalServices
        //    {
        //        //配置ORM缓存
        //        DataInfoCacheService = new SqlSugarCache()
        //    };
        //}

        //注入SqlSugarClient
        services.AddScoped<ISqlSugarClient>(x =>
        {
            return new SqlSugarClient(configs, db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "----" + pars);
                };
            });
        });
        //IOC注入
 //services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
        //注入泛型仓储
        // services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
        services.AddScoped(typeof(IOrderRepository<>), typeof(OrderRepository<>));
        services.AddScoped(typeof(IProductRepository<>), typeof(ProductRepository<>));
        //services.AddScoped(typeof(IAICFlowRepository<List<ISqlSugarClient>>), typeof(AICFlowRepository<List<ISqlSugarClient>>));
        return services;
    }
}
